package reco.db.validation;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import reco.db.Constraint;

public class InConstraintFactory<E> extends SimpleConstraintFactory implements DateConstraintFactory, EnumConstraintFactory, NumberConstraintFactory,
                StringConstraintFactory {
    private final E[] elements;

    public InConstraintFactory(E... elements) {
        super(expectedTypeOn(elements));
        this.elements = elements.clone();
    }

    private static Class<?> expectedTypeOn(Object[] elements) {
        checkNotNull(elements, "elements");
        checkArgument(elements.length > 0, "elements can not be empty");
        return elements[0].getClass();
    }

    @Override
    protected <T> Constraint<T> createConstraint() {
        @SuppressWarnings("unchecked")
        final Constraint<T> constraint = (Constraint<T>) new InConstraint<E>(this.elements);

        return constraint;
    }
}